package com.englishstudymachine;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.VideoView;
//import io.vov.vitamio.widget.VideoView;
public class MyVideoAndCharacterControler   {
	VideoView myVideo;
	MyCharacterGenerator myCharacter;
	boolean isRun;
	Thread a;
	Handler showCharacterHandler;
	Message msg;
	Bundle data;
	Timer timer;
	long runDelay=50;
	
	long currentTime;
	int lastNum;
	int currentNum;
	MyCharacter currentCharacter;
	public MyVideoAndCharacterControler(VideoView video,MyCharacterGenerator character,Handler showCharacterHandler)
	{
		myVideo=video;
		myCharacter=character;
		isRun=true;
		this.showCharacterHandler=showCharacterHandler;
		msg=new Message();
		data=new Bundle();
		msg.setData(data);
	
	}
	public void Start()
	{
		 currentTime=0;
		 lastNum=0;
		 currentNum=0;
		TimerTask task =new TimerTask() {   
		    public void run() {   
		    	try
		    	{
		    	currentTime= myVideo.getCurrentPosition()-2000;
				currentNum=myCharacter.getNumByTime(currentTime);
				if(currentNum==-1&& currentNum!=lastNum && lastNum!=-1)
				{
					sendShowCharacter(-1,currentTime);
					lastNum=currentNum;
				}
				if (currentNum!=-1 && currentNum!=lastNum)
				{
					sendShowCharacter(currentNum,currentTime);
					lastNum=currentNum;
				}
				
				
				
		    	}catch(Exception e){System.out.println(e.toString());}
		    }   
		};   
		 timer = new Timer();
		 timer.schedule(task,0, runDelay);
		
	}
	public void Stop()
	{
		timer.cancel();
	}
	public void sendShowCharacter(int num,long currenttime)
	{
		msg=new Message();
		data=new Bundle();
		data.putInt("ShowCharacter", num);
		data.putLong("CurrentTime", currenttime);
		msg.setData(data);
		showCharacterHandler.sendMessage(msg);
		
	}
	
	
}
